
//  XJBaseRequest.m
//  XJMCustomer
//  Created by Mr.hu on 2017/10/9.
//  Copyright © 2017年 Mr.hu. All rights reserved.

#import "XJBaseRequest.h"

@implementation XJBaseRequest
@synthesize callbackBlock;
-(void)startWithBlock:(requestResultBlock)block{
    if (block) {
        callbackBlock = block;
    }
    [super start];
}

-(void)updateData:(NSData *)data dataName:(NSString *)dataName withBlock:(requestResultBlock)block{
    if (block) {
        callbackBlock = block;
    }
    //组合请求URL
    NSString *requestURLString = [NSString stringWithFormat:@"%@%@", self.host, self.actionKey];
    
    //交互式请求需要加入UserId
    if (self.access) {
        NSString *accessToken = [XJToolClass getUserId];
        [self.agrs setObject:accessToken forKey:@"0-account"];
    }
    NSURLSessionDataTask *request;
    request = [[XJAFNCliect sharedClient] dataRequest:requestURLString parameters:self.agrs data:data dataName:dataName  completion:^(id result, NSError *error) {
        if (result) {
            [self resolveData:result];
        } else {
            NSLog(@"ERROR: %@", error);
            [self resolveError];
        }
    }];
}

-(void)updateImages:(NSArray *)images names:(NSArray *)names withBlock:(requestResultBlock)block{
    if (block) {
        callbackBlock = block;
    }
    //组合请求URL
    NSString *requestURLString = [NSString stringWithFormat:@"%@%@", self.host, self.actionKey];
    
    //交互式请求需要加入UserId
    if (self.access) {
        NSString *accessToken = [XJToolClass getUserId];
        [self.agrs setObject:accessToken forKey:@"0-account"];
    }
    NSURLSessionDataTask *request;
    request = [[XJAFNCliect sharedClient] dataRequest:requestURLString parameters:self.agrs datas:images dataNames:names  completion:^(id result, NSError *error) {
        if (result) {
            [self resolveData:result];
        } else {
            NSLog(@"ERROR: %@", error);
            [self resolveError];
        }
    }];
}

-(void)updateImagesAndVoice:(NSArray *)images VoiceUrl:(NSString *)voiceUrl withBlock:(requestResultBlock)block{
    if (block) {
        callbackBlock = block;
    }
    //组合请求URL
    NSString *requestURLString = [NSString stringWithFormat:@"%@%@", self.host, self.actionKey];
    //交互式请求需要加入UserId
    if (self.access) {
        NSString *accessToken = [XJToolClass getUserId];
        [self.agrs setObject:accessToken forKey:@"0-account"];
    }
    NSURLSessionDataTask *request;
    request = [[XJAFNCliect sharedClient] imgVoiceRequest:requestURLString parameters:self.agrs datas:images Voice:voiceUrl completion:^(id result, NSError *error) {
        if (result) {
            [self resolveData:result];
        } else {
            NSLog(@"ERROR: %@", error);
            [self resolveError];
        }
    }];
}

//解析返回数据
-(void)resolveData:(id)data
{
    NSMutableDictionary *dic = [self toArrayOrNSDictionary:data];
    
    NSLog(@"json:%s",[[dic description]UTF8String]);
    if (dic==nil) {
        NSLog(@"there is no datas");
        callbackBlock(nil,@"未知错误");
        return;
    }
    
    NSInteger status = [[dic objectForKey:@"code"]integerValue];
    if([[dic allKeys] containsObject:@"code"]){
        status=[[dic objectForKey:@"code"]integerValue];
    }
    
    //智能客服接口专用判断
    if([[dic allKeys] containsObject:@"answers"]){
        status=0;
    }
    //成功返回  返回-2其它地方bao cuo
    if (status == 0) {
        if (callbackBlock) {
            
            callbackBlock([XJToolClass changeType:dic],nil);
        }
        //        if (callbackBlock) {
        //            callbackBlock([dic objectForKey:@"sessionBean"],nil);
        //        }
    }else if (status == 10008){
        
        if (callbackBlock) {
            callbackBlock([XJToolClass changeType:dic],nil);
        }
        
    }else if (status == -2){
        
        if (callbackBlock) {
            callbackBlock([XJToolClass changeType:dic],nil);
        }
    }
    
    //返回错误信息码
    //    else {
    //        NSString *errorMsg =[NSString stringWithFormat:@"%@",[dic objectForKey:@"status"]];
    //        NSLog(@"错误信息=%@",errorMsg);
    //        if (callbackBlock) {
    //            callbackBlock(nil,[ToolClass getResponsedErrorDescriptionWithErrorCode:errorMsg]);
    //        }
    //    }
    else{
        if ([dic[@"message"] isEqualToString:@"还未登录"]) {
            
        }else if ([dic[@"message"] isEqualToString:@"设备不一致，需要发送验证码，调用发送验证码接口"]){
            if (callbackBlock) {
                callbackBlock(dic,dic[@"message"]);
            }
        }
        else{
            if (callbackBlock) {
                callbackBlock(nil,dic[@"message"]);
            }
        }
    }
    
}
//处理错误
-(void)resolveError
{
    if (callbackBlock) {
        callbackBlock(nil,@"亲，网络断了哦，请检查网络连接");
    }
    
}
@end
